<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>29. react 中的受控组件</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="app"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom 用于支持 react 操作 dom -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入 label，用于将 jsx 转化为 js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入 prop-types，用于对于组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <!-- 此处必须写 text/babel -->
    <script type="text/babel">

        // 受控组件

        // 创建组件
        class Login extends React.Component {

            state = { username: '', password: '' }

            handleSubmit = (event) => {
                event.preventDefault()  // 阻止默认事件，即表单提交组织
                
                const { username, password } = this.state
                
                alert(`你输入的账号是 ${username}，你输入的密码是 ${password}`)
            }

            saveUsername = (event) => {
                // console.log(event.target.value)
                this.setState({ username: event.target.value })
            }

            savePassword = (event) => {
                // console.log(event.target.value)
                this.setState({ password: event.target.value })
            }


            render() {
                return (
                    <form action="http://www.baidu.com" onSubmit={this.handleSubmit}>
                        账号：<input onChange={this.saveUsername} type="text" name="username" /> <br />
                        密码：<input onChange={this.savePassword} type="passwofd" name="password" /><br />
                        <button>登 录</button>
                    </form>
                )
            }
        }
        // 渲染组件
        ReactDOM.render(<Login />, document.getElementById("app"))
    </script>

</body>

</html>